package com.yangkunhong.dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.math.BigInteger;
import java.sql.SQLException;

/**
 * ClassName: TransationDao
 * Description:
 *
 * @author yangkunhong
 * @date 2024/6/27 9:46
 */
public class TransActionDao extends NewBaseDao{

    public void addUserAndLog(){
        startTransaction();
        try{
           add("insert into user(username,password,createAt,createIp) values(?,?,?,?)",
                   "yangkunhong01","123456","2024-07-01 01:01:01","1270.0.1");
            add("insert into user(username,password,createAt,createIp) values(?,?,?,?)",
                    "yangkunhong02","123456","2024-07-01 01:01:01","1270.0.1");
            commit();
        }catch (Exception e){
            System.out.println("事务回滚操作");
            rollback();
        }
    }
    public  Integer  add(String insertSql, Object... params) throws SQLException {

        QueryRunner queryRunner = new QueryRunner();

        BigInteger insert;

        insert = queryRunner.insert(connection,insertSql, new ScalarHandler<>(), params);

        return insert.intValue();

    }

}
